﻿using System.Linq;
using System.Web.UI;

namespace NativeCode.Web.Controls.Renderers
{
	public class CheckBoxSelectorRenderer<TDataItem> : ControlRenderer
		where TDataItem : new()
	{
		public CheckBoxSelectorRenderer(HtmlTextWriter writer) : base(writer)
		{
		}

		public CheckBoxSelectorRendererOptions Options { get; private set; }
		public CheckBoxSelector<TDataItem> CheckBoxSelector { get; set; }

		public void RenderCheckBoxSelectorControl(CheckBoxSelector<TDataItem> checkBoxSelector,
		                                          CheckBoxSelectorRendererOptions checkBoxSelectorRendererOptions)
		{
			CheckBoxSelector = checkBoxSelector;
			Options = checkBoxSelectorRendererOptions;

			HtmlWriter writer = new HtmlWriter(HtmlTextWriter);

			CheckBoxSelector.CssNames.Add("checkbox-selector");

			writer
				.Tag(HtmlTextWriterTag.Div)
					.Attribute(HtmlTextWriterAttribute.Class, CheckBoxSelector.CssClass)
					.Call(RenderCheckBoxItems)
				.EndTag();
		}

		public void RenderCheckBoxItems(HtmlWriter writer)
		{
			int count = CheckBoxSelector.CheckBoxSelectorItems.Count();
			for (int i=0; i < count; i++)
			{
				bool isFirstItem = (i == 0);
				bool isLastItem = (i == count - 1);
				bool isNotFirstOrLastIItem = (!isFirstItem && !isLastItem);

				CheckBoxSelectorItem checkBoxSelectorItem = CheckBoxSelector.CheckBoxSelectorItems.ElementAt(i);

				writer
					.Tag(HtmlTextWriterTag.Div)
						.AttributeIf(HtmlTextWriterAttribute.Class, "row first", isFirstItem)
						.AttributeIf(HtmlTextWriterAttribute.Class, "row last", isLastItem)
						.AttributeIf(HtmlTextWriterAttribute.Class, "row", isNotFirstOrLastIItem)
						.Call(RenderCheckBoxItem, checkBoxSelectorItem, i)
					.EndTag();
			}
		}

		public void RenderCheckBoxItem(HtmlWriter writer, CheckBoxSelectorItem checkBoxSelectorItem, int index)
		{
			bool leftSide = Options.LabelPosition == LabelPosition.Left;
			bool rightSide = Options.LabelPosition == LabelPosition.Right || Options.LabelPosition == LabelPosition.Default;
			bool hasLabelText = !string.IsNullOrEmpty(checkBoxSelectorItem.LabelText);
			bool hasToolTipText = !string.IsNullOrEmpty(checkBoxSelectorItem.ToolTipText);
			bool hasValueText = !string.IsNullOrEmpty(checkBoxSelectorItem.ValueText);
			string labelControlId = string.Format("{0}_{1}_{2}", CheckBoxSelector.ClientID, "Label", index);
			string inputControlId = string.Format("{0}_{1}_{2}", CheckBoxSelector.ClientID, "Input", index);

			writer
				.TagIf(HtmlTextWriterTag.Label, leftSide)
					.Attribute(HtmlTextWriterAttribute.Id, labelControlId)
					.Attribute(HtmlTextWriterAttribute.Name, labelControlId)
					.Attribute(HtmlTextWriterAttribute.Class, "label")
					.Attribute(HtmlTextWriterAttribute.For, inputControlId)
					.Write(checkBoxSelectorItem.LabelText)
				.EndTag()
				.Tag(HtmlTextWriterTag.Input)
					.Attribute(HtmlTextWriterAttribute.Id, inputControlId)
					.Attribute(HtmlTextWriterAttribute.Name, inputControlId)
					.Attribute(HtmlTextWriterAttribute.Class, "checkbox")
					.Attribute(HtmlTextWriterAttribute.Type, "checkbox")
					.AttributeIf(HtmlTextWriterAttribute.Value, checkBoxSelectorItem.ValueText, hasValueText)
					.AttributeIf("display-text", checkBoxSelectorItem.LabelText, hasLabelText)
					.AttributeIf(HtmlTextWriterAttribute.Title, checkBoxSelectorItem.ToolTipText, hasToolTipText)
				.EndTag()
				.TagIf(HtmlTextWriterTag.Label, rightSide)
					.Attribute(HtmlTextWriterAttribute.Id, labelControlId)
					.Attribute(HtmlTextWriterAttribute.Name, labelControlId)
					.Attribute(HtmlTextWriterAttribute.Class, "label")
					.Attribute(HtmlTextWriterAttribute.For, inputControlId)
					.Write(checkBoxSelectorItem.LabelText)
				.EndTag();
		}
	}
}